import com.itmd.dao.OrderMapper;
import com.itmd.dao.UserMapper;
import com.itmd.domain.Order;
import com.itmd.domain.User;
import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
import org.junit.Before;
import org.junit.Test;

import java.io.IOException;
import java.io.InputStream;
import java.util.Date;
import java.util.List;

public class MyBatisTest2 {

    private OrderMapper mapper;


    @Before
    public void before() throws IOException {
        InputStream resourceAsStream = Resources.getResourceAsStream("sqlMapConfig.xml");
        SqlSessionFactory build = new SqlSessionFactoryBuilder().build(resourceAsStream);
        SqlSession sqlSession = build.openSession(true);
        mapper = sqlSession.getMapper(OrderMapper.class);
    }





    @Test
    public void addOrder() {

        Order order = new Order();
        System.out.println(new Date());
        order.setOrdertime(new Date());
        order.setTotal("100");
//        order.setUid(1);
        User user = new User();
        user.setId(1);
        order.setUser(user);
        mapper.addOrder(order);
    }

    @Test
    public void testfindAll2() {
        List<Order> orderList = mapper.findAll2();
        for(Order o : orderList) {
            System.out.println(o);
        }
    }

    @Test
    public void testfindAllorders() {
      List<Order> orderList = mapper.findAllorders();
      for(Order o : orderList) {
          System.out.println(o);
      }

    }

    @Test
    public void testfindAll () {

        List<Order> orderlist = mapper.findAll();

        for(Order order : orderlist ) {

            System.out.println(order);
        }

    }


}
